#!/bin/bash
# Script for running Judge0 workers.
#
# Usage: ./scripts/workers
#
source ./scripts/load-config
export | sudo tee /api/environment

run_resque=1
resque_pid=0
scheduler_pid=0

date_now() {
    echo -n $(date +"%Y-%m-%d-%H-%M-%S")
}

exit_gracefully() {
    echo "[$(date_now)] Killing workers."
    run_resque=0
    kill -SIGQUIT $(pgrep -P $resque_pid)
    kill -SIGTERM $resque_pid
}

trap exit_gracefully SIGTERM SIGINT

mkdir -p tmp/pids &> /dev/null
while [[ $run_resque -eq 1 ]]; do
    echo "[$(date_now)] Starting scheduler."
    if ! ps -p $scheduler_pid &> /dev/null; then
        rake resque:scheduler &
        scheduler_pid=$!
    fi

    rm -rf tmp/pids/resque.pid &> /dev/null
    echo "[$(date_now)] Starting workers."
    rails resque:workers &
    resque_pid=$!
    while ps -p $resque_pid > /dev/null; do sleep 1s; done
    echo "[$(date_now)] Workers are stopped."
done
